﻿using System;
using RayDen.Library.Components.SystemComponents;
using RayDen.Library.Core;
using RayDen.Library.Core.Primitives;
using RayDen.RayEngine.Core.Interface;
using RayDen.RayEngine.Core.Types;
using RayDen.RayEngine.Data;
using RayDen.RayEngine.Samplers;

namespace RayDen.RayEngine
{

    public interface IContextSampler<in TEngineContext>
    {
        void SetContext(TEngineContext context);
    }

    public class PathSamplerBase : IPathSampler
    {
        protected internal int RayIndex;
        protected internal RayData PathRay;
        protected internal PathBufferBase pathIntegrator;
        protected LightSample[] ls;

        public RgbSpectrum Radiance;
        public Sample Sample;

        internal SurfaceSampler SurfaceSampler;
        internal LightSampler  LightSampler;

        public virtual void InitPath(IPathProcessor buffer)
        {
            this.pathIntegrator = (PathBufferBase) buffer;
        }
        public virtual bool FillRayBuffer(RayBuffer rayBuffer)
        {
            return false;
        }
        public virtual void Advance(RayBuffer rayBuffer, SampleBuffer consumer) { }
        //public virtual void Advance(RayBuffer rayBuffer, SampleBuffer sampleBuffer, Action<PathSamplerBase> onRestart) { }

        public virtual void Splat(SampleBuffer sampleBuffer)
        {
#if VERBOSE
            this.pathIntegrator.splattedCount++;
#endif
            //if (!this.Radiance.IsBlack())
            {
                sampleBuffer.SplatSample(this.Sample.imageX, this.Sample.imageY, ref this.Radiance);
            }
            this.InitPath(pathIntegrator);
        }


        protected virtual bool ShadowRayTest(ref RayHit shadowRayHit, ref RayData shadowRay, out RgbSpectrum attenuation, out bool continueTrace)
        {
            attenuation = new RgbSpectrum(1f);
            continueTrace = false;
            return shadowRayHit.Index == 0xffffffffu;
        }

        protected static void Print(string msg)
        {
#if VERBOSE
            Tracer.TraceLine(msg);
#endif

        }

    }
}